package base.shiro.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.*;

/**
 * 配置同级目录web目录为静态文件放置
 */
@Configuration
@EnableWebMvc
public class FileConfig implements WebMvcConfigurer {

  @Override
  public void addCorsMappings(CorsRegistry registry) {
    registry.addMapping("/**").allowedHeaders("*").allowedOrigins("*").allowCredentials(true)
        .allowedMethods("GET", "POST", "DELETE", "PUT", "OPTIONS", "PATCH").maxAge(1728000L);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
//    registry.addInterceptor(new LoginInterceptor())
//            .addPathPatterns("/**").excludePathPatterns(
//        "/","/rest/public/v2.0/auth/token","/web/**","/error", "/swagger-ui.html",
//        "/train/weeklyplan/getWeekDetail2",
//        "/train/weeklyplan/info/**",
//        "/openapi/2.0/list",
//        "/openapi/2.0/page/list",
//        "/openapi/2.0/save",
//        "/doc.html", "/doc.html/**", "/webjars/**",
//        "/swagger-resources/**", "/swagger-ui.html/**", "/gs-guide-websocket/**");
  }


  @Override
  public void addResourceHandlers(ResourceHandlerRegistry registry) {
//    String oldpath = AppSettingsUtil.getUploadHome();
    registry.addResourceHandler("swagger-ui.html")
        .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/web/**")
        .addResourceLocations("file:web/");
    registry.addResourceHandler("/static/**")
        .addResourceLocations("file:web/static/");

    registry.addResourceHandler("/webjars/**")
        .addResourceLocations("classpath:/META-INF/resources/webjars/");

//    registry.addResourceHandler("/files/**")
//        .addResourceLocations("file:" + oldpath);
  }

  @Override
  public void addViewControllers(ViewControllerRegistry registry) {
    registry.addRedirectViewController("/", "/web/index.html");
    registry.setOrder(-2147483648);
  }

}
